using System;
using System.IO;

namespace Devsoft.Utilities
{
	/// <summary>
	/// Represents a logging class.
	/// </summary>
	public class Log
	{		
		

        #region Instance
        /// <summary>
        /// Holds the singleton instance
        /// </summary>
        protected static Log _Instance;
        /// <summary>
        /// Gets the singleton instance.
        /// </summary>
        /// <value>The singleton instance.</value>
        public static Log Instance
        {
        	get
        	{
        		if(_Instance == null)
        			_Instance = new Log();
        		return _Instance;
        	}
        }
        #endregion





        /// <summary>
        /// Initializes the log file.
        /// </summary>
        /// <param name="w">The the log file's StreamWriter.</param>
		public  void InitLog(StreamWriter w)
		{
			if (!_bInitialized)
			{
				w.WriteLine("");
				_bInitialized = true;
				w.Write("\r\nLog Entry : ");
				w.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(),
					DateTime.Now.ToLongDateString());
				w.WriteLine ("-------------------------------");
                _bInitialized = true;
			}//End If
		}

        /// <summary>
        /// Gets the name of the log file.
        /// </summary>
        /// <returns></returns>
		public  string GetFileName()
		{
			return Path.GetExePath() + "logs\\" + System.DateTime.Now.ToString("ddMMyy") + ".txt";

		}

        /// <summary>
        /// Appends a message the log file.
        /// </summary>
        /// <param name="sMsg">The text.</param>
		public  void  AppendLogs(string sMsg)
		{           
            if (!_Enable)
            {
                return;
            }//End If
            lock (this)
            {
                string path = GetFileName();
                using (StreamWriter w = File.AppendText(path))
                {

                    InitLog(w);
                    w.WriteLine("[DEBUG] {0}", sMsg);
                    // Update the underlying file.
                    w.Flush();
                }
            }
			
		}
        /// <summary>
        /// Appends a message the log file.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="sMsg">The text.</param>
		public  void  AppendLogs(object sender,string sMsg)
		{
            if (!_Enable)
            {
                return;
            }//End If
            lock (this)
            {
                string path = GetFileName();
                using (StreamWriter w = File.AppendText(path))
                {

                    InitLog(w);
                    w.WriteLine("[{0}] : {1}", sender.GetType().FullName, sMsg);
                    // Update the underlying file.
                    w.Flush();
                }
            }
			
		}

        /// <summary>
        /// Appends a message the log file.
        /// </summary>
        /// <param name="o">The sender.</param>
        /// <param name="e">The exception.</param>
		public  void AppendLogs(object o, System.Exception e)
		{
            if (!_Enable)
            {
                return;
            }//End If
            lock (this)
            {
                string path = GetFileName();
                using (StreamWriter w = File.AppendText(path))
                {

                    InitLog(w);

                    w.WriteLine("[Sender]  :{0}(Value: {1})", o.GetType().FullName,o);
                    w.WriteLine("[EXCEPTION]  :{0}", e.Message);
                    w.WriteLine("source: " + e.Source);
                    w.WriteLine("Stack: " + e.StackTrace);
                    w.WriteLine(String.Empty);
                    // Update the underlying file.
                    w.Flush();
                }
            }
		}

        /// <summary>
        /// Appends a message the log file.
        /// </summary>
        /// <param name="e">The exception.</param>
		public  void AppendLogs(System.Exception e)
		{
            if (!_Enable)
            {
                return;
            }//End If
            lock (this)
            {
                string path = GetFileName();
                using (StreamWriter w = File.AppendText(path))
                {

                    InitLog(w);

                    w.WriteLine("[EXCEPTION]  :{0}", e.Message);
                    w.WriteLine("source: " + e.Source);
                    w.WriteLine("Stack: " + e.StackTrace);
                    w.WriteLine(String.Empty);
                    // Update the underlying file.
                    w.Flush();
                }
            }
		}

        #region Property Enable
        /// <summary>
        /// Holds the currently status of this instance.
        /// </summary>
        protected bool _Enable;
        /// <summary>
        /// Gets or sets a value indicating whether this <see cref="Log"/> is enable.
        /// </summary>
        /// <value><c>true</c> if enable; otherwise, <c>false</c>.</value>
        public bool Enable
        {
        	set
        	{
        		_Enable = value;
        	}
        	get
        	{
        		return _Enable;
        	}
        }
        #endregion

        /// <summary>
        /// Holds the state of this instance
        /// </summary>
        /// <value><c>true</c> if Initialized; otherwise, <c>false</c>.</value>
        protected bool _bInitialized = false;
        /// <summary>
        /// Initializes a new instance of the <see cref="Log"/> class.
        /// </summary>
        protected Log()
        {
            //
            // TODO: Add constructor logic here
            //

#if MONODROID
            _Enable = false;
#else
            _Enable = true;
#endif
        }



	}
}
